package com.earstep.connectionpool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erastep.util.PropertyUtil;

public class ConnectionPoolFactory {
	public static Log LOG = LogFactory.getLog(ConnectionPoolFactory.class);
	public static ConnectionPoolFactory factory;
	private Map<String, DataSource> pools;

	/**
	 * 构造方法 实例化配置文件中 所有连接池
	 */
	private ConnectionPoolFactory() {
		pools = new HashMap<String, DataSource>();
		Properties connection = PropertyUtil.loadProperties("/connection.properties");
		Enumeration<Object> keys = connection.keys();
		while (keys.hasMoreElements()) {
			String name = "" + keys.nextElement();
			Properties properties = PropertyUtil.loadProperties(connection.getProperty(name));
			PoolBean bean = new PoolBean(properties.getProperty("url"), name,
					properties.getProperty("driverClassName"),
					properties.getProperty("initialSize"),
					properties.getProperty("maxActive"),
					properties.getProperty("maxWait"),
					properties.getProperty("removeAbandonedTimeout"),
					properties.getProperty("removeAbandoned"),
					properties.getProperty("logAbandoned"),
					properties.getProperty("maxIdle"),
					properties.getProperty("minIdle"));
			initDataSource(bean);
			LOG.info(name + " 连接池创建成功！");
		}
	}

	/**
	 * 连接池单例模式
	 * 
	 * @return
	 */
	public static ConnectionPoolFactory getInstance() {
		if (factory == null) {
			factory = new ConnectionPoolFactory();
		}
		return factory;
	}

	/**
	 * 初始化连接池的数据源
	 * 
	 * @param poolBean
	 */
	public DataSource initDataSource(PoolBean poolBean) {
		Properties properties = new Properties();
		DataSource dataSource = null;
		try {
			properties.setProperty("driverClassName", poolBean.getDriver());
			properties.setProperty("url", poolBean.getUrl());
			properties.setProperty("maxActive", poolBean.getMaxActive());
			properties.setProperty("maxWait", poolBean.getMaxWait());
			properties.setProperty("initialSize", poolBean.getInitialSize());
			properties.setProperty("maxIdle", poolBean.getMaxIdle());

			properties.setProperty("logAbandoned", poolBean.getLogAbandoned());
			properties.setProperty("minIdle", poolBean.getMinIdle());
			properties.setProperty("removeAbandoned",
					poolBean.getRemoveAbandoned());// true,false
			properties.setProperty("removeAbandonedTimeout",
					poolBean.getRemoveAbandonedTimeout());// 秒
			dataSource = (BasicDataSource) BasicDataSourceFactory
					.createDataSource(properties);
			pools.put(poolBean.getDatabasename(), dataSource);
		} catch (Exception e) {
			LOG.error("Init pool error, " + e);
		}
		return dataSource;
	}

	/**
	 * 从连接池 根据配置名称获取连接
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public Connection getConnection(String name) throws SQLException {
		return pools.get(name).getConnection();
	}

	/**
	 * 根据名称 销毁连接池
	 * 
	 * @param name
	 * @throws SQLException
	 */
	public void closePool(String name) throws SQLException {
		((BasicDataSource) pools.get(name)).close();
		pools.remove(name);
	}

}
